package com.javaclimb.accounting.controller;

import com.alibaba.fastjson.JSONObject;
import com.javaclimb.accounting.po.Account;
import com.javaclimb.accounting.po.Bind;
import com.javaclimb.accounting.po.Card;
import com.javaclimb.accounting.po.User;
import com.javaclimb.accounting.service.AccountService;
import com.javaclimb.accounting.service.BindService;
import com.javaclimb.accounting.service.CardService;
import com.javaclimb.accounting.utils.Consts;
import com.javaclimb.accounting.vo.CardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: XX
 * @create: 2022-12-28 09:02
 **/

@RestController
@RequestMapping("/accounting")
public class CardController {
    @Autowired
    private CardService cardService;
    @Autowired
    private BindService bindService;
    @Autowired
    private AccountService accountService;

    //http://localhost:8888/accounting/card/find?userAccId=?
    @RequestMapping(value = "/card/all",method = RequestMethod.POST)
    public JSONObject findBindCard(User user){
        JSONObject jsonObject=new JSONObject();
        /*根据当前用户的id查询电子账号信息*/
        Account account = accountService.findByUserAccId(user.getUserAccId());
        if (account==null){
            /*如果电子账户为空说明该用户不存在电子账户*/
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "该用户不存在电子账户");
        }else {
            /*如果当前用户存在电子账户则根据当前电子账户id查看绑定的卡*/
            List<Bind> binds = bindService.findBindsByAccountId(account.getAccId());
            /*如果当前账户没有绑定银行卡则返回错误信息*/
            if (binds.size() == 0) {
                jsonObject.put(Consts.CODE, 0);
                jsonObject.put(Consts.MSG, "该用户没有绑定的银行卡,请先绑定银行卡");
            } else {
                List<CardVo> cards = new ArrayList<>();
                for (int i = 0; i < binds.size(); i++) {
                    /*根据绑定的银行卡的id去card表中查找银行卡信息*/
                    cards.add(cardService.findCardById(binds.get(i).getBangCardId()));
                }
                jsonObject.put(Consts.CODE, 1);
                jsonObject.put(Consts.MSG, "该用户拥有绑定的银行卡");
                jsonObject.put("cards", cards);
            }
            jsonObject.put("account",account);
        }
        return jsonObject;
    }

    //http://localhost:8888/accounting/card/find
    /*查看card表中是否存在当前输入的卡号和所属银行*/
    @RequestMapping(value = "/card/find",method = RequestMethod.POST)
    public JSONObject findCard(Card card){
        Card card1 = cardService.findCard(card);
        JSONObject jsonObject=new JSONObject();
        if (card1==null){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"不存在当前卡号或所属银行");
        }else {
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"查询成功");
        }
        return jsonObject;
    }

    //http://localhost:8888/accounting/card/bind
    /*查询card表中银行卡信息和用户输入的信息是否匹配（用户信息身份验证）*/
    @RequestMapping(value = "/card/bind",method = RequestMethod.POST)
    public JSONObject bindCard(Card card,Account account){
        Card card1=cardService.authentication(card);
        JSONObject jsonObject=new JSONObject();
        if (card1==null){
            //如果card表中信息与用户输入信息不匹配则验证失败
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"身份验证失败");
        }else {
            System.out.println(card1);
            /*如果在bind表中查到该卡id则表示该卡已被绑定*/
            Bind bind1=bindService.findBind(card1.getCardId());

            if (bind1==null) {
                Bind bind=new Bind();
                /*如果在card表中能够查到该银行卡，但是在bind表中查不到该银行卡，则在bind中插入数据表示绑定成功*/
                bind.setBangName(card1.getCardBank());
                bind.setBangAccId(account.getAccId());
                bind.setBangCardId(card1.getCardId());
                System.out.println(bind);
                int count = bindService.bind(bind);
                jsonObject.put(Consts.CODE, 1);
                jsonObject.put(Consts.MSG, "绑定成功");
            }else {
                jsonObject.put(Consts.CODE,0);
                jsonObject.put(Consts.MSG,"该卡已被绑定");
            }
        }
        return jsonObject;
    }

}
